package com.project.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class AllInterceptor implements HandlerInterceptor {

	@Override	
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		// TODO Auto-generated method stub
		
		HttpSession session = request.getSession();
		if (null == session.getAttribute("userId")) {
			// response.sendRedirect("/login.jsp");
			return true;
		} else {
			return true;
		}
		
//		String requestUri = request.getRequestURI();
//      String contextPath = request.getContextPath();
//      String url = requestUri.substring(contextPath.length());
//      System.out.println(url);
		
		 /**  
         * 这个主要是判断当前连接的是不是业务类的控制器，  
         * 如果是springmvc静态资源的handler，  
         * 在转成HandlerMethod时就会报错.  
         *   
         */  
//        if(HandlerMethod.class.equals(handler.getClass())){
//            //获取controller，判断是不是实现登录接口的控制器 
//            HandlerMethod method = (HandlerMethod) handler;
//            Object controller = method.getBean();
//        }
	}

	@Override
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		// TODO Auto-generated method stub
	}

	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		// TODO Auto-generated method stub
	}

}
